{include file='header'/}
<title>菜单列表</title>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm layuimini-btn-primary btn-refresh"><i class="fa fa-refresh"></i></button>
            <a class="layui-btn layui-btn-normal layui-btn-sm {if !check_auth('system.menu/add')}layui-hide{/if}" data-open="{:__url('system.menu/add')}" data-title="添加菜单">
                <i class="fa fa-plus"></i> 添加菜单</a>
            <button id="btnExpandAll" class="layui-btn layui-btn-sm layuimini-btn-primary ">
                <i class="layui-icon">&#xe668;</i>展开全部
            </button>
            <button id="btnFoldAll" class="layui-btn layui-btn-sm layuimini-btn-primary">
                <i class="layui-icon">&#xe66b;</i>折叠全部
            </button>
        </div>
        <table id="munu-table" lay-filter="munu-table"></table>
    </div>
</div>
<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" value="{{d.status}}" lay-skin="switch" lay-filter="switchStatus"
           data-href="{:__url('system.menu/modify')}?id={{d.id}}&field=status" lay-text="启用|禁用" {{ d.status== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="iconTpl">
    <i class="fa {{d.icon}}"></i>
</script>
<script type="text/html" id="optionTpl">
    {{#  if(d.id !==1){ }}
    <a class="layui-btn layui-btn-xs layui-btn-normal {if !check_auth('system.menu/add')}layui-hide{/if}" data-open="{:__url('system.menu/add')}?id={{d.id}}"
       data-title="添加下级" data-full="true">添加下级</a>
    <a class="layui-btn layui-btn-xs layui-btn-success {if !check_auth('system.menu/edit')}layui-hide{/if}" data-open="{:__url('system.menu/edit')}?id={{d.id}}"
       data-title="编辑" data-full="true">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {if !check_auth('system.menu/delete')}layui-hide{/if}" data-delete="{:__url('system.menu/delete')}?id={{d.id}}"
       data-title="确定要删除吗？" data-reload="1">删除</a>
    {{#  } }}
</script>

{include file='footer'/}
<script>
  layui.use(['table', 'treetable'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var treetable = layui.treetable;

    // 渲染表格
    layer.load(2);
    var insTb = treetable.render({
      treeColIndex: 1,
      treeSpid: 0,
      homdPid: 99999999,
      treeIdName: 'id',
      treePidName: 'pid',
      elem: '#munu-table',
      method: 'get',
      url: "{:__url('system.menu/index')}",
      page: false,
      cols: [[
        {type: 'numbers'},
        {field: 'title', minWidth: 200, title: '权限名称'},
        {field: 'icon', width: 80, title: '图标', templet: '#iconTpl', align: 'center'},
        {field: 'href', title: '菜单url'},
        {
          field: 'is_home',
          width: 80,
          title: '类型',
          align: 'center',
          templet: function (d) {
            if (d.pid === 99999999) {
              return '<span class="layui-badge layui-bg-blue">首页</span>';
            }
            if (d.pid === 0) {
              return '<span class="layui-badge layui-bg-orange">模块</span>';
            } else {
              return '<span class="layui-badge-rim">菜单</span>';
            }
          }
        },
        {field: 'status', title: '状态', width: 95, templet: '#switchTpl', align: 'center'},
        {field: 'sort', width: 80, title: '排序', edit: 'text', align: 'center'},
        {
          width: 200,
          title: '操作',
          templet: '#optionTpl',
          align: 'center',
        }
      ]],
      done: function () {
        layer.closeAll('loading');
      }
    });

    /* 监听单元格编辑 */
    table.on('edit(munu-table)', function (obj) {
      var value = obj.value
        , data = obj.data
        , field = obj.field;
      $.post("{:__url('system.menu/modify')}", {id: data.id, val: value, field: field}, function (res) {
        if (res.code === 1) {
          layer.msg(res.msg, {time: 1500, icon: 1}, function () {
            if (res.data.refresh == 1) {
              window.parent.location.reload();
            }
          });
        } else {
          layer.msg(res.msg, {time: 1500, icon: 2});
        }
      })
    });
    //全部展开
    $('#btnExpandAll').click(function () {
      treetable.expandAll('#munu-table');
    });

    // 全部折叠
    $('#btnFoldAll').click(function () {
      treetable.foldAll('#munu-table');
    });


    /*$('#btn-expand').click(function () {
      treetable.expandAll('#munu-table');
    });

    $('#btn-fold').click(function () {
      treetable.foldAll('#munu-table');
    });

    //监听工具条
    table.on('tool(munu-table)', function (obj) {
      var data = obj.data;
      var layEvent = obj.event;

      if (layEvent === 'del') {
        layer.msg('删除' + data.id);
      } else if (layEvent === 'edit') {
        layer.msg('修改' + data.id);
      }
    });*/
  });
</script>
</body>
</html>
